package org.aplikator.client.widgets;

import java.util.Date;

import org.aplikator.client.Aplikator.AplikatorResources;
import org.aplikator.client.descriptor.PropertyDTO;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DatePicker;

public class DateFieldWidget extends DataFieldBase<Date> implements DataField<Date> {
    private DateBox box = new DateBox(new DatePicker(), null, new DateBox.DefaultFormat(DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT)));

    public DateFieldWidget(String caption, PropertyDTO<Date> property, boolean labelAbove) {
        super(caption, property, false, labelAbove);
        add(box);
        box.addStyleName(PaneWidgetResources.INSTANCE.css().formData());
        box.addValueChangeHandler(new ValueChangeHandler<Date>() {
            public void onValueChange(ValueChangeEvent<Date> event) {
                setDirty(true);
            }
        });
    }

    public void setValue(Date value) {
        box.setValue(value);
    }

    public Date getValue() {
        return box.getValue();
    }

    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler) {
        return box.addValueChangeHandler(handler);
    }

    public void setEnabled(boolean enabled) {
        box.setEnabled(enabled);
    }

    public void setDirty(boolean dirty) {
        super.setDirty(dirty);
        if (dirty) {
            box.addStyleName(AplikatorResources.INSTANCE.css().dirty());
        } else {
            box.removeStyleName(AplikatorResources.INSTANCE.css().dirty());
        }
    }

}
